<template>
  <div class="address-form">
    <div class="form-container">
      <h3>{{ editing ? '编辑地址' : '添加新地址' }}</h3>

      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="name">收货人</label>
          <input
            id="name"
            v-model="formData.name"
            type="text"
            required
            placeholder="请输入收货人姓名"
          />
        </div>

        <div class="form-group">
          <label for="phone">联系电话</label>
          <input
            id="phone"
            v-model="formData.phone"
            type="tel"
            required
            placeholder="请输入联系电话"
          />
        </div>

        <div class="form-row">
          <div class="form-group">
            <label for="province">省份</label>
            <input
              id="province"
              v-model="formData.province"
              type="text"
              required
              placeholder="请输入省份"
            />
          </div>

          <div class="form-group">
            <label for="city">城市</label>
            <input
              id="city"
              v-model="formData.city"
              type="text"
              required
              placeholder="请输入城市"
            />
          </div>
        </div>

        <div class="form-row">
          <div class="form-group">
            <label for="district">区县</label>
            <input
              id="district"
              v-model="formData.district"
              type="text"
              required
              placeholder="请输入区县"
            />
          </div>
        </div>

        <div class="form-group">
          <label for="detail">详细地址</label>
          <input
            id="detail"
            v-model="formData.detail"
            type="text"
            required
            placeholder="请输入详细地址"
          />
        </div>

        <div class="form-actions">
          <button type="button" class="cancel-btn" @click="handleClose">
            取消
          </button>
          <button type="submit" class="submit-btn">
            {{ editing ? '保存' : '添加' }}
          </button>
        </div
>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue';

const props = defineProps({
  address: {
    type: Object as () => Address,
    default: null
  }
});

const emit = defineEmits(['close', 'save']);

interface Address {
  id?: number;
  name: string;
  phone: string;
  province: string;
  city: string;
  district: string;
  detail: string;
  isDefault?: boolean;
}

const formData = ref<Address>({
  name: '',
  phone: '',
  province: '',
  city: '',
  district: '',
  detail: ''
});

const editing = computed(() => !!props.address?.id);

watch(
  () => props.address,
  (newVal) => {
    if (newVal) {
      formData.value = { ...newVal };
    } else {
      resetForm();
    }
  },
  { immediate: true }
);

const resetForm = () => {
  formData.value = {
    name: '',
    phone: '',
    province: '',
    city: '',
    district: '',
    detail: ''
  };
};

const handleSubmit = () => {
  emit('save', formData.value);
};

const handleClose = () => {
  emit('close');
};
</script>

<style scoped>
.address-form {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
}

.form-container {
  background: white;
  padding: 24px;
  border-radius: 8px;
  width: 500px;
  max-width: 90%;
}

h3 {
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 18px;
  color: #333;
}

.form-group {
  margin-bottom: 16px;
}

.form-row {
  display: flex;
  gap: 16px;
}

.form-row .form-group {
  flex: 1;
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #666;
}

input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.cancel-btn {
  padding: 8px 16px;
  background: white;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
}

.submit-btn {
  padding: 8px 16px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>
